package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.model.vx.bo.WxCartCheckedBo;
import com.cskaoyan.model.vx.bo.WxCartUpdateBo;
import com.cskaoyan.model.vx.vo.CartTotalEntity;
import com.cskaoyan.model.vx.vo.WxCartListVo;
import com.cskaoyan.model.vx.vo.WxCheckoutVo;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitRetryTemplateCustomizer;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description:
 * @author: 牛犇
 * @date 2021/12/2 10:42
 */

@Service
public class WxCartServiceImpl implements WxCartService {

    @Autowired
    MarketCartMapper marketCartMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Override
    public WxCartListVo getCartIndex(Integer userId) {
        WxCartListVo wxCartListVo = getWxCartListVo(userId);
        return wxCartListVo;
    }


    @Override
    public WxCartListVo getCartChecked(WxCartCheckedBo wxCartCheckedBo) {
        List<Integer> productIds = wxCartCheckedBo.getProductIds();
        for (Integer productId : productIds) {
            if (wxCartCheckedBo.getIsChecked() == 0) {
                marketCartMapper.updateCheckedToFalseByProductId(productId);
            } else {
                marketCartMapper.updateCheckedToTrueByProductId(productId);
            }

        }
        return getWxCartListVo();
    }

    @Override
    public WxCartListVo deleteCartChecked(WxCartCheckedBo wxCartCheckedBo) {
        List<Integer> productIds = wxCartCheckedBo.getProductIds();
        for (Integer productId : productIds) {
            marketCartMapper.deleteGoodsByProductId(productId);
        }
        WxCartListVo wxCartListVo = getWxCartListVo();
        return wxCartListVo;
    }

    @Override
    public void updateCartGood(WxCartUpdateBo wxCartUpdateBo) {
        MarketCart marketCart = new MarketCart();
        marketCart.setId(wxCartUpdateBo.getId());
        marketCart.setProductId(wxCartUpdateBo.getProductId());
        marketCart.setGoodsId(wxCartUpdateBo.getGoodsId());
        marketCart.setNumber(wxCartUpdateBo.getNumber());
        MarketCartExample example = new MarketCartExample();
        example.createCriteria().andIdEqualTo(wxCartUpdateBo.getId());
        marketCartMapper.updateByExampleSelective(marketCart, example);
    }

    @Override
    public int addCartGood(WxCartUpdateBo wxCartUpdateBo, Integer userId) {

        //商品
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.createCriteria().andIdEqualTo(wxCartUpdateBo.getGoodsId()).andDeletedEqualTo(false);
        List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExample(marketGoodsExample);
        MarketGoods marketGoods = marketGoodsList.get(0);
        //商品种类和价格
        MarketGoodsProductExample example2 = new MarketGoodsProductExample();
        example2.createCriteria().andIdEqualTo(wxCartUpdateBo.getProductId()).andDeletedEqualTo(false);
        List<MarketGoodsProduct> marketGoodsProductList = marketGoodsProductMapper.selectByExample(example2);
        MarketGoodsProduct marketGoodsProduct = marketGoodsProductList.get(0);
        //购物车
        MarketCart marketCart = new MarketCart();
        marketCart.setNumber(wxCartUpdateBo.getNumber());
        marketCart.setUserId(userId);
        marketCart.setGoodsId(wxCartUpdateBo.getGoodsId());
        marketCart.setGoodsSn(marketGoods.getGoodsSn());
        marketCart.setGoodsName(marketGoods.getName());
        marketCart.setProductId(wxCartUpdateBo.getProductId());
        marketCart.setPrice(marketGoodsProduct.getPrice());
        marketCart.setSpecifications(marketGoodsProduct.getSpecifications());
        marketCart.setChecked(true);
        marketCart.setPicUrl(marketGoods.getPicUrl());
        MarketCartExample marketCartExample = new MarketCartExample();
        marketCartExample.createCriteria().andDeletedEqualTo(false);
        List<MarketCart> marketCartList = marketCartMapper.selectByExample(marketCartExample);
        for (MarketCart cart : marketCartList) {
            if (cart.getProductId() == wxCartUpdateBo.getProductId()) {
                MarketCart marketCart1 = new MarketCart();
                BigDecimal addCartNumber = new BigDecimal(wxCartUpdateBo.getNumber());

                marketCartExample.createCriteria().andProductIdEqualTo(cart.getProductId());
                List<MarketCart> marketCartList1 = marketCartMapper.selectByExample(marketCartExample);
                short number = marketCartList1.get(0).getNumber();
                number += (short) wxCartUpdateBo.getNumber();
                marketCart1.setNumber(number);
                marketCartMapper.updateByExampleSelective(marketCart1, marketCartExample);
                MarketCartExample example = new MarketCartExample();
                example.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
                int count = (int) marketCartMapper.countByExample(example);
                return count;
            }
        }
        marketCartMapper.insertCart(marketCart);
        MarketCartExample example = new MarketCartExample();
        example.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(userId);
        int count = (int) marketCartMapper.countByExample(example);

        return count;
    }

    @Override
    public int countCart(Integer userId) {
        if (userId == null) {
            return 0;
        }
        MarketCartExample example = new MarketCartExample();
        example.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        int count = (int) marketCartMapper.countByExample(example);
        return count;
    }

    @Override
    public int fastAddCartGoods(WxCartUpdateBo wxCartUpdateBo, Integer userId) {
        //商品
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.createCriteria().andIdEqualTo(wxCartUpdateBo.getGoodsId()).andDeletedEqualTo(false);
        List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExample(marketGoodsExample);
        MarketGoods marketGoods = marketGoodsList.get(0);
        //商品种类和价格
        MarketGoodsProductExample example2 = new MarketGoodsProductExample();
        example2.createCriteria().andIdEqualTo(wxCartUpdateBo.getProductId()).andDeletedEqualTo(false);
        List<MarketGoodsProduct> marketGoodsProductList = marketGoodsProductMapper.selectByExample(example2);
        MarketGoodsProduct marketGoodsProduct = marketGoodsProductList.get(0);
        //购物车
        MarketCart marketCart = new MarketCart();
        marketCart.setNumber(wxCartUpdateBo.getNumber());
        marketCart.setUserId(userId);
        marketCart.setGoodsId(wxCartUpdateBo.getGoodsId());
        marketCart.setGoodsSn(marketGoods.getGoodsSn());
        marketCart.setGoodsName(marketGoods.getName());
        marketCart.setProductId(wxCartUpdateBo.getProductId());
        marketCart.setPrice(marketGoodsProduct.getPrice());
        marketCart.setSpecifications(marketGoodsProduct.getSpecifications());
        marketCart.setChecked(true);
        marketCart.setPicUrl(marketGoods.getPicUrl());
        MarketCartExample marketCartExample = new MarketCartExample();
        marketCartExample.createCriteria().andDeletedEqualTo(false);
        List<MarketCart> marketCartList = marketCartMapper.selectByExample(marketCartExample);
        for (MarketCart cart : marketCartList) {
            if (cart.getProductId() == wxCartUpdateBo.getProductId()) {
                MarketCart marketCart1 = new MarketCart();
                marketCart1.setNumber(wxCartUpdateBo.getNumber());
                marketCartMapper.updateByExampleSelective(marketCart1, marketCartExample);
                return cart.getId();
            }
        }
        marketCartMapper.insertCart(marketCart);


        return marketCart.getId();
    }

    @Override
    public WxCheckoutVo checkOutCart(Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId, Integer userId) {
        //1.获得地址信息
        MarketAddress marketAddress = new MarketAddress();

        MarketAddressExample marketAddressExample = new MarketAddressExample();
        //先按照前端发送的address进行查询，如果查到了就用前端的，否则使用默认地址
        marketAddressExample.createCriteria().andUserIdEqualTo(userId).
                andDeletedEqualTo(false).andIdEqualTo(addressId);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        if(marketAddresses.size() != 0){
            marketAddress = marketAddresses.get(0);
        }else {
            //如果等于0，使用默认地址
            MarketAddressExample marketAddressExample1 = new MarketAddressExample();
            marketAddressExample1.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
            List<MarketAddress> marketAddresses1 = marketAddressMapper.selectByExample(marketAddressExample1);
            for (MarketAddress address : marketAddresses1) {
                marketAddress = address;
                if(address.getIsDefault() == true){  //如果是默认地址，跳出循环
                    break;
                }
            }
        }

        //2.获得当前购买物品的信息
        MarketCart marketCart = new MarketCart();
        MarketCartExample exampleCart = new MarketCartExample();
        List<MarketCart> marketCartList = null;
        BigDecimal goodTotalPrice=new BigDecimal(0);
        if (cartId != 0) {
            exampleCart.createCriteria().andIdEqualTo(cartId).andDeletedEqualTo(false);
            marketCartList = marketCartMapper.selectByExample(exampleCart);
            marketCart = marketCartList.get(0);
            BigDecimal goodNumber = new BigDecimal(marketCart.getNumber());
            goodTotalPrice = goodTotalPrice.add(marketCart.getPrice().multiply(goodNumber));
        }
        if(cartId==0){
            exampleCart.createCriteria().andCheckedEqualTo(true).andDeletedEqualTo(false);
            marketCartList = marketCartMapper.selectByExample(exampleCart);
            for (MarketCart cart : marketCartList) {
                BigDecimal goodNumber=new BigDecimal(cart.getNumber());
                BigDecimal singleGoodTotalPrice=cart.getPrice().multiply(goodNumber);
                goodTotalPrice=goodTotalPrice.add(singleGoodTotalPrice);
            }
        }
        //3.2货运价钱 先默认为0
        BigDecimal freightPrice = new BigDecimal(0);
        //3.3 优惠券减免
        MarketCoupon marketCoupon = new MarketCoupon();
        MarketCouponExample exampleCoupon = new MarketCouponExample();
        if (couponId != 0&&couponId!=-1) {
            exampleCoupon.createCriteria().andIdEqualTo(couponId).andDeletedEqualTo(false);
            List<MarketCoupon> marketCouponList = marketCouponMapper.selectByExample(exampleCoupon);
            marketCoupon = marketCouponList.get(0);
        }
        BigDecimal couponPrice = new BigDecimal(0);
        if (marketCoupon.getDiscount() != null) {
            couponPrice=couponPrice.add(marketCoupon.getDiscount());
        }

        //3.4 团购券减免 先默认为0
        BigDecimal grouponPrice = new BigDecimal(0);
        //3.5 订单实际价格 和订单总价格 先默认为一样 不知道有什么区别  等于商品总价格减去优惠券、运费、团购券价格
        BigDecimal actualPrice = goodTotalPrice.subtract(freightPrice).subtract(couponPrice).subtract(grouponPrice);
        BigDecimal orderTotalPrice = goodTotalPrice.subtract(freightPrice).subtract(couponPrice).subtract(grouponPrice);
        //4.获得可用的优惠卷数量  不知道该怎么做 TODO
        int availableCouponLength = 7;
        WxCheckoutVo wxCheckoutVo = new WxCheckoutVo(grouponRulesId, cartId, userCouponId, couponId, addressId, availableCouponLength, actualPrice, orderTotalPrice, goodTotalPrice, couponPrice, freightPrice, grouponPrice, marketAddress, marketCartList);
        return wxCheckoutVo;
    }

    private WxCartListVo getWxCartListVo() {
        //所有的商品
        MarketCartExample example = new MarketCartExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<MarketCart> marketCartList = marketCartMapper.selectByExample(example);
        int goodsCount = (int) marketCartMapper.countByExample(example);
        BigDecimal goodsAmount = new BigDecimal(0);
        for (MarketCart marketCart : marketCartList) {
            goodsAmount.add(marketCart.getPrice());
        }
        //被选中的商品
        MarketCartExample example1 = new MarketCartExample();
        example1.createCriteria().andCheckedEqualTo(true).andDeletedEqualTo(false);
        int checkedGoodsCount = (int) marketCartMapper.countByExample(example1);
        List<MarketCart> marketCartList1 = marketCartMapper.selectByExample(example1);
        BigDecimal checkedGoodsAmount = new BigDecimal(0);
        for (MarketCart marketCart1 : marketCartList1) {
            checkedGoodsAmount.add(marketCart1.getPrice());
        }
        CartTotalEntity cartTotalEntity = new CartTotalEntity(goodsCount, checkedGoodsCount, goodsAmount, checkedGoodsAmount);

        WxCartListVo wxCartListVo = new WxCartListVo(cartTotalEntity, marketCartList);
        return wxCartListVo;
    }

    private WxCartListVo getWxCartListVo(Integer userId) {
        //所有的商品
        MarketCartExample example = new MarketCartExample();
        example.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        example.setOrderByClause("id desc");
        List<MarketCart> marketCartList = marketCartMapper.selectByExample(example);
        int goodsCount = (int) marketCartMapper.countByExample(example);
        BigDecimal goodsAmount = new BigDecimal(0);
        for (MarketCart marketCart : marketCartList) {
            goodsAmount.add(marketCart.getPrice());
        }
        //被选中的商品
        MarketCartExample example1 = new MarketCartExample();
        example1.createCriteria().andCheckedEqualTo(true).andDeletedEqualTo(false);
        int checkedGoodsCount = (int) marketCartMapper.countByExample(example1);
        List<MarketCart> marketCartList1 = marketCartMapper.selectByExample(example1);
        BigDecimal checkedGoodsAmount = new BigDecimal(0);
        for (MarketCart marketCart1 : marketCartList1) {
            checkedGoodsAmount.add(marketCart1.getPrice());
        }
        CartTotalEntity cartTotalEntity = new CartTotalEntity(goodsCount, checkedGoodsCount, goodsAmount, checkedGoodsAmount);

        WxCartListVo wxCartListVo = new WxCartListVo(cartTotalEntity, marketCartList);
        return wxCartListVo;
    }


}
